<?php
/**
 * 
 * @author tangbl
 * 
 */
class ApiAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index() {
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("Api");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "1 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->field("description,content",true)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			$opdel     = L('ato_form_ele_deal');
			
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'          => $row['id'],
						'title'       => $row['title'],
						'appid'       => $row['appid'],
						'appkey'      => $row['appkey'],
						'times_round' => $row['times']."/".$row['round'],
						'addtime'     => date('Y-m-d',$row['addtime']),
						'isdel'       => $row['isdel']==1?L('ato_form_ele_status2'):L('ato_form_ele_status1'),
						'deal'=>'
						 <a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
						 <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>
						 <a href="'.__URL__.'/access/id/'.$row['id'].'" class="btn green icn-only" title="'.$opdel.'" data-id="'.$row['id'].'"><i class="icon-group"></i></a>
							 '
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			$this->display();
		}	
	}
	
	/**
	 * 添加
	 */
	public function add(){
		if(IS_POST){
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"cache"  => true,
			);
			parent::do_add("Api",$param);
		} else {
			$lang = $this->_param("lang","",getLang());
			$this->assign("lang",$lang);
			
			$cateid  = $this->_param('cateid','intval',0);
			$this -> assign("cateid",$cateid);

			$this -> assign("jumpUrl",U('Api/add'));
			$this -> display();
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		
		$param = array(
			"reply"  => true,
			"cache"  => true,
		);
		parent::do_del("Api",false,$param);
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		if(IS_POST) {
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"cache"  => true,
			);
			parent::do_edit("Api",$param);
		}else{
			$model   = D("Api");
			$id        = $this->_param('id','intval',0);
			if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
			
			$data      = $model->find($id); 
			$this->assign("data",$data);
						
			$module    = "Api";
			$single    = find_thumb($data['solevar'],'single',$module);
			$this->assign("single",$single);

			$this->display();
		}
	}
	
	/**
	 * 设置api权限
	 */
	public function access(){
		
		$model = D("Api");
		$forid = $this->_param("id");
		$view  = $model->find($forid);
		
		load("@.api");
		$arr = return_array();
		
		if(IS_POST){
			
			$map['forid'] = $view['id'];
			M('ApiAccess')->where($map)->delete();
			
			$data = $_POST;
			
			if($data['chk']){
				foreach($data['chk'] as $k=>$v){
					foreach($v as $n=>$m){
						
						$map['appid']       = $view['appid'];
						$map['addid']       = session(C('USER_AUTH_KEY'));;
						$map['module']      = $k;
						$map['action']      = $n;
						$map['addtime']     = time();
						$map['visit_times'] = $data['visit'][$k][$n];
						M('ApiAccess')->add($map);
						
					}
				}
			}
			
			$this->assign("jumpUrl",U('Api/index'));
			$this->success(L('cfg_form_tipmsg_editsucc'));
			
		}else{
			
			$html = $model->getApiNodeHtml($arr,$view);
			
			$this->assign("html",$html);
			$this->display();
		}
		
	}
}
?>